package com.dailyblue.java;

import lombok.SneakyThrows;

import java.io.File;

// File
public class DemoA {

    // 如何定义一个文件或者目录
    private static void a() {
        // a.txt的抽象  文件
        File file1 = new File("/Users/dailyblue/a/a.txt");
        File file2 = new File("/Users/dailyblue/a", "a.txt");
        // 目录 (文件夹)
        File file3 = new File("/Users/dailyblue/a");
        File file4 = new File("/Users/dailyblue", "a");
        File file5 = new File(file3, "a.txt");
        // 可以代表一个不存在的文件或者目录
        File file6 = new File(file4, "b"); // 不存在
        System.out.println(file1);
        System.out.println(file2);
        System.out.println(file3);
        System.out.println(file4);
        System.out.println(file5);
        System.out.println(file6);
    }

    @SneakyThrows
    // 文件或者目录的操作--创建
    public static void b1() {
        File file1 = new File("/Users/dailyblue/a/b.txt");
        File file2 = new File("/Users/dailyblue/a/c/a");
        // 创建文件
        // file1.createNewFile();
        boolean b = file2.mkdirs(); // mkdirs和mkdir的区别？
        System.out.println(b);
    }

    @SneakyThrows
    // 文件或者目录的操作--删除
    public static void b2() {
        File file1 = new File("/Users/dailyblue/a/b.txt");
        File file2 = new File("/Users/dailyblue/a/c");
        // 不进入回收站，删除时要谨慎，删除目录时，必须保证目录中为空
        file1.deleteOnExit();
        boolean b = file2.delete();
        System.out.println(b);
    }

    @SneakyThrows
    // 文件或者目录的操作--获取父子内容
    public static void b3() {
        File file1 = new File("/Users/dailyblue/a/b.txt");
        File file2 = new File("/Users/dailyblue/a/c");
        // 获取父目录
        String parentPath1 = file1.getParent();
        File parent1 = file1.getParentFile();
        String parentPath2 = file2.getParent();
        File parent2 = file2.getParentFile();
        System.out.println(parentPath1);
        System.out.println(parent1);
        System.out.println(parentPath2);
        System.out.println(parent2);
        // 获取子内容--只针对于目录
        String[] names = file2.list();
        File[] child = file2.listFiles();
        for (String s : names) {
            System.out.println(s);
        }
        for (File c : child) {
            System.out.println(c);
        }
    }

    // 其他操作
    private static void c() {
        File file = new File("/Users/dailyblue/a/a.txt");
        boolean 是否存在 = file.exists();
        boolean 是否是文件 = file.isFile();
        boolean 是否是目录 = file.isDirectory();
        System.out.println(是否存在);
        System.out.println(是否是文件);
        System.out.println(是否是目录);
    }

    public static void main(String[] args) {
        c();
    }
}
